package org.systemsbiology.bigquery.gae.servlets;

import com.google.gson.JsonObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Logger;

import static com.google.appengine.api.channel.ChannelServiceFactory.getChannelService;
import static org.systemsbiology.bigquery.gae.dao.Channels.newChannelKey;

/**
 * @author hrovira
 */
public class ChannelsServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(ChannelsServlet.class.getName());

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String channelKey = newChannelKey(request, response);
            log.info("channelKey=" + channelKey);

            JsonObject json = new JsonObject();
            json.addProperty("token", getChannelService().createChannel(channelKey));
            response.setContentType("application/json");
            response.getWriter().write(json.toString());
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

}
